package com.muli.muwai.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfFinBuget;
import com.muli.muwai.entity.IntfFinProductionCost;
import com.muli.muwai.exception.MyDefinedEnum;
import com.muli.muwai.mapper.FinancingBudgetMapper;
import com.muli.muwai.service.FinancingBudget;
import com.muli.muwai.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.DelegatingServerHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FinancingBudgetImpl extends ServiceImpl<FinancingBudgetMapper, IntfFinBuget> implements FinancingBudget {
    @Autowired
    FinancingBudgetMapper financingBudgetMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fanance(List<String[]> fanance) throws Exception {
        //转换成实体类对象
        List<IntfFinBuget> list = getIntfFinBugets(fanance);

        //定义长度集合
        List<Integer> num = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        //分被获取1-12月列的值 确认哪月份有数据 有则删除数据后导入 以长度判断哪个月有有效数据
        Set<Double> jan = list.stream().map(IntfFinBuget -> IntfFinBuget.getJan()).collect(Collectors.toSet());
        Set<Double> feb = list.stream().map(IntfFinBuget -> IntfFinBuget.getFeb()).collect(Collectors.toSet());
        Set<Double> mar = list.stream().map(IntfFinBuget -> IntfFinBuget.getMar()).collect(Collectors.toSet());
        Set<Double> apr = list.stream().map(IntfFinBuget -> IntfFinBuget.getApr()).collect(Collectors.toSet());
        Set<Double> may = list.stream().map(IntfFinBuget -> IntfFinBuget.getMay()).collect(Collectors.toSet());
        Set<Double> jun = list.stream().map(IntfFinBuget -> IntfFinBuget.getJun()).collect(Collectors.toSet());
        Set<Double> jul = list.stream().map(IntfFinBuget -> IntfFinBuget.getJul()).collect(Collectors.toSet());
        Set<Double> aug = list.stream().map(IntfFinBuget -> IntfFinBuget.getAug()).collect(Collectors.toSet());
        Set<Double> sep = list.stream().map(IntfFinBuget -> IntfFinBuget.getSept()).collect(Collectors.toSet());
        Set<Double> oct = list.stream().map(IntfFinBuget -> IntfFinBuget.getOct()).collect(Collectors.toSet());
        Set<Double> nov = list.stream().map(IntfFinBuget -> IntfFinBuget.getNov()).collect(Collectors.toSet());
        Set<Double> dec = list.stream().map(IntfFinBuget -> IntfFinBuget.getDecember()).collect(Collectors.toSet());
        //加入map
        map.put("jan", jan.size());
        map.put("feb", feb.size());
        map.put("mar", mar.size());
        map.put("apr", apr.size());
        map.put("may", may.size());
        map.put("jun", jun.size());
        map.put("jul", jul.size());
        map.put("aug", aug.size());
        map.put("sep", sep.size());
        map.put("oct", oct.size());
        map.put("nov", nov.size());
        map.put("dec", dec.size());
        //加入list
        num.add(jan.size());
        num.add(feb.size());
        num.add(mar.size());
        num.add(apr.size());
        num.add(may.size());
        num.add(jun.size());
        num.add(jul.size());
        num.add(aug.size());
        num.add(sep.size());
        num.add(oct.size());
        num.add(nov.size());
        num.add(dec.size());

        //获取最大值
        Integer max = Collections.max(num);
        //返回值代表有效数据所代表的月份
        String key = getKey(map, max);


        //判断其他月份是否有误写的数据 如果有抛出异常
        //怎么获取最大值之外的值
        //筛选
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            if (!entry.getKey().equals(key)) {
                if (entry.getValue() != 0) {
                    throw new Exception(MyDefinedEnum.ERROR_MONTHDAY.getMsg());
                }
            }
        }

        //要求为上传单月数据其他月份数据不得传 测试数据所有月都有数据,故而报错
        //测试请注掉
        switch (key) {
            case "jan":
                int jan1 = financingBudgetMapper.deleteJan();
                saveBatch(list);
                System.out.println(jan1);
                break;
            case "feb":
                int feb1 = financingBudgetMapper.deleteFeb();
                saveBatch(list);
                System.out.println(feb1);
                break;
            case "mar":
                int mar1 = financingBudgetMapper.deleteMar();
                saveBatch(list);
                System.out.println(mar1);
                break;
            case "apr":
                int apr1 = financingBudgetMapper.deleteApr();
                saveBatch(list);
                System.out.println(apr1);
                break;
            case "may":
                int may1 = financingBudgetMapper.deleteMay();
                saveBatch(list);
                System.out.println(may1);
                break;
            case "jun":
                int jun1 = financingBudgetMapper.deleteJun();
                saveBatch(list);
                System.out.println(jun1);
                break;
            case "jul":
                int jul1 = financingBudgetMapper.deleteJul();
                saveBatch(list);
                System.out.println(jul1);
                break;
            case "aug":
                int aug1 = financingBudgetMapper.deleteAug();
                saveBatch(list);
                System.out.println(aug1);
                break;
            case "sep":
                int sep1 = financingBudgetMapper.deleteSep();
                saveBatch(list);
                System.out.println(sep1);
                break;
            case "oct":
                int oct1 = financingBudgetMapper.deleteOct();
                saveBatch(list);
                System.out.println(oct1);
                break;
            case "nov":
                int nov1 = financingBudgetMapper.deleteNov();
                saveBatch(list);
                System.out.println(nov1);
                break;
            case "dec":
                int dec1 = financingBudgetMapper.deleteDec();
                saveBatch(list);
                System.out.println(dec1);
                break;
        }
        //测试数据用
        //saveBatch(list);
    }

    private List<IntfFinBuget> getIntfFinBugets(List<String[]> fanance) throws Exception {
        List<IntfFinBuget> list = new ArrayList<>();
        //错误列记录
        int num=0;
        for (String[] strings : fanance) {
            IntfFinBuget intfFinBuget = new IntfFinBuget();
            intfFinBuget.setFiscalYear(strings[0]);
            intfFinBuget.setProfitCenter((strings[1]));
            intfFinBuget.setAmountType((strings[2]));
            intfFinBuget.setLevel1((strings[3]));
            intfFinBuget.setLevel2((strings[4]));
            intfFinBuget.setLevel3((strings[5]));
            try {
                num=7;
                // intfFinBuget.setJan((strings[6]);
                if (strings[6].equals("")) {
                    intfFinBuget.setJan(0);
                } else {
                    intfFinBuget.setJan(Double.parseDouble(strings[6]));
                }
                num=8;
                //intfFinBuget.setFeb((strings[7]);
                if (strings[7].equals("")) {
                    intfFinBuget.setFeb(0);
                } else {
                    intfFinBuget.setFeb(Double.parseDouble(strings[7]));
                }
                num=9;
                // intfFinBuget.setMar((strings[8]);
                if (strings[8].equals("")) {
                    intfFinBuget.setMar(Double.parseDouble(String.valueOf(0)));
                } else {
                    intfFinBuget.setMar(Double.parseDouble(strings[8]));
                }
                num=10;
                //intfFinBuget.setApr((strings[9]);
                if (strings[9].equals("")) {
                    intfFinBuget.setApr(0);
                } else {
                    intfFinBuget.setApr(Double.parseDouble(strings[9]));
                }
                num=11;
                // intfFinBuget.setMay((strings[10]);
                if (strings[10].equals("")) {
                    intfFinBuget.setMay(0);
                } else {
                    intfFinBuget.setMay(Double.parseDouble(strings[10]));
                }
                num=12;
                // intfFinBuget.setJun((strings[11]);
                if (strings[11].equals("")) {
                    intfFinBuget.setJun(0);
                } else {
                    intfFinBuget.setJun(Double.parseDouble(strings[11]));
                }
                num=13;
                // intfFinBuget.setJul((strings[12]);
                if (strings[12].equals("")) {
                    intfFinBuget.setJul(0);
                } else {
                    intfFinBuget.setJul(Double.parseDouble(strings[12]));
                }
                num=14;
                // intfFinBuget.setAug((strings[13]);
                if (strings[13].equals("")) {
                    intfFinBuget.setAug(0);
                } else {
                    intfFinBuget.setAug(Double.parseDouble(strings[13]));
                }
                num=15;
                // intfFinBuget.setSept((strings[14]);
                if (strings[14].equals("")) {
                    intfFinBuget.setSept(0);
                } else {
                    intfFinBuget.setSept(Double.parseDouble(strings[14]));
                }
                num=16;
                // intfFinBuget.setOct((strings[15]);
                if (strings[15].equals("")) {
                    intfFinBuget.setOct(0);
                } else {
                    intfFinBuget.setOct(Double.parseDouble(strings[15]));
                }
                num=17;
                // intfFinBuget.setNov((strings[16]));
                if (strings[16].equals("")) {
                    intfFinBuget.setNov(0);
                } else {
                    intfFinBuget.setNov(Double.parseDouble(strings[16]));
                }
                num=18;
                // intfFinBuget.setDecember((strings[17]));
                if (strings[17].equals("")) {
                    intfFinBuget.setDecember(0);
                } else {
                    intfFinBuget.setDecember(Double.parseDouble(strings[17]));
                }
            } catch (Exception e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + num + "列,格式错误");
            }


            try {
                // intfFinBuget.setCreatedTime((strings[18]));
                SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
                Date date1 = sdf.parse(strings[18]);
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                intfFinBuget.setCreatedTime(timestamp1);
            } catch (Exception e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + 19 + "列,请设写成yyyy/m/d格式");
            }


            intfFinBuget.setCreatedBy((strings[19]));

            try {
                //intfFinBuget.setUpdatedTime((strings[20]));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yy/MM/dd");
                Date date2 = sdf1.parse(strings[20]);
                Timestamp timestamp2 = new Timestamp(date2.getTime());
                intfFinBuget.setUpdatedTime(timestamp2);
            } catch (Exception e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + 21 + "列,请设写成yyyy/m/d格式");
            }
            intfFinBuget.setUpdatedBy((strings[21]));
            list.add(intfFinBuget);

        }
        return list;
    }

    private String getKey(HashMap<String, Integer> map, Integer value) {
        String key = null;

        for (String getKey : map.keySet()) {
            if (map.get(getKey).equals(value)) {
                key = getKey;
            }
        }
        return key;
    }
}
